package com.jxt.myjxtmanager.ui.activity;

import android.os.Handler;
import android.os.Message;
import android.support.design.widget.TextInputEditText;
import android.support.v7.widget.AppCompatButton;
import android.text.TextUtils;
import android.view.View;

import com.jxt.myjxtmanager.BaseActivity;
import com.jxt.myjxtmanager.R;
import com.jxt.myjxtmanager.db.SaleUser;
import com.jxt.myjxtmanager.db.SaleUserModel;
import com.jxt.myjxtmanager.utils.ToastUtil;
import com.jxt.myjxtmanager.views.CustomProgressDialog;

/**
 * 修改密码类
 * Created by qm on 2017/2/9 0009.
 */
public class ChangePswActivity extends BaseActivity {

    private TextInputEditText edit_forget_username;
    private TextInputEditText edit_forget_password;
    private TextInputEditText edit_verify_code;
    private AppCompatButton ibtn_login;
    private String newAgainPsw;
    private String newPsw;
    private String oldPsw;
    private CustomProgressDialog dialog;
    private SaleUser saleUser;


    @Override
    protected int getLayoutId() {
        return R.layout.activity_change_psw;
    }

    @Override
    protected void initView() {
        setToolbarTitle("修改密码");
        setToolbarNavigationIcon(R.drawable.ic_left_return);
        edit_forget_username = (TextInputEditText) findViewById(R.id.edit_forget_username);
        edit_forget_password = (TextInputEditText) findViewById(R.id.edit_forget_password);
        edit_verify_code = (TextInputEditText) findViewById(R.id.edit_verify_code);
        ibtn_login = (AppCompatButton) findViewById(R.id.ibtn_login);

        ibtn_login.setOnClickListener(this);

    }

    @Override
    public void onClickLeftMenu() {
        super.onClickLeftMenu();
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.ibtn_login:
                if (verifyData()) {
                    loadData();
                }
                break;
        }
    }

    /**
     * 验证输入信息完整及正确性
     *
     * @return true  符合要求   false  不符合要求  原因 Toast会输出
     */
    private boolean verifyData() {
        saleUser = SaleUserModel.getInstance().loadCurrentSaleUsers();
        oldPsw = edit_forget_username.getText().toString();
        newPsw = edit_forget_password.getText().toString();
        newAgainPsw = edit_verify_code.getText().toString();
        if (TextUtils.isEmpty(oldPsw) || TextUtils.isEmpty(newPsw) || TextUtils.isEmpty(newAgainPsw)) {//不能为空
            ToastUtil.showToast(this, "请检查信息输入完整性");
            return false;
        } else if (!TextUtils.equals(newPsw, newAgainPsw)) {//密码输入是否一致
            ToastUtil.showToast(this, "新密码输入不一致");
            return false;
        } else if (saleUser != null && !TextUtils.equals(oldPsw, saleUser.getPassword())) {//原来密码是否输入正确
            ToastUtil.showToast(this, "原密码输入错误");
            return false;
        }
        return true;

    }

    /**
     * 验证成功后  执行保存密码请求操作
     */
    private void loadData() {
        dialog = new CustomProgressDialog(this, "");
        if (dialog != null) {
            dialog.showDialog();
        }
        //模拟网络加载
        Message message = new Message();
        message.arg1 = 2;
        loadDataHandler.sendMessageDelayed(message, 1000);
    }

    /**
     * 保存密码操作后 在主线程操作数据  并finish掉当前activity
     */
    private Handler loadDataHandler = new Handler() {
        @Override
        public void handleMessage(Message msg) {
            if (dialog != null) {
                dialog.dismiss();
            }
            SaleUser saleUser = SaleUserModel.getInstance().loadCurrentSaleUsers();
            saleUser.setPassword(newPsw);
            SaleUserModel.getInstance().insertOrReplace(saleUser);
            ToastUtil.showToast(ChangePswActivity.this, "修改成功");
            finish();
        }
    };
}
